<template>
  <el-row>
    <el-col class="login-wrappers">
      <div class="input-box">
        <el-form :model="loginModel" ref="login" :rules="loginRules">
          <!-- <nb-form
            v-model="loginModel.loginName"
            placeholder="输入登录账号"
            :maxs="64"
          ></nb-form> -->
          <el-form-item label="" label-width="0" prop="loginName">
            <el-input placeholder="输入登录账号" prefix-icon="el-icon-user" ref="username" @keyup.enter.native="changeFocus(1)" v-model="loginModel.loginName"></el-input>
          </el-form-item>
          <el-form-item label="" label-width="0" prop="password">
            <el-input placeholder="输入密码" prefix-icon="el-icon-lock" ref="password" @keyup.enter.native="changeFocus(2)" type="password" v-model="loginModel.password"></el-input>
          </el-form-item>
          <!-- <nb-form
            v-model="loginModel.password"
            placeholder="输入密码"
            type="password"
            required=":输入密码"
          > -->
<!--            <el-row slot="in" @click.native="forget" class="right-box box1">-->
<!--              忘记密码？-->
<!--            </el-row>-->
          <!-- </nb-form> -->
        </el-form>

      </div>
      <div class="btn-box">
        <el-button class="confirm-btn" type="primary" @click="confirmLogin">登录</el-button>
      </div>
    </el-col>
  </el-row>
</template>
<script>
  import md5 from 'md5'
  import NbForm from '@/components/nb-form.vue'
  import Vue from 'vue'
  export default {
    data () {
      return {
        code: '',
        captcha: '',
        loginModel: {
          loginName: '',
          password: ''
        },
        loginRules: {
          loginName: [
            {required: true, trigger: 'blur', message: '账号不能为空'}
          ],
          password: [
            {required: true, trigger: 'blur', message: '密码不能为空'}
          ]
        },
        loading: null
      }
    },
    components: {
      NbForm
    },
    methods: {
      changeFocus (step) {
        if (step === 1) {
          this.$refs['password'].focus()
        } else if (step === 2) {
          this.confirmLogin()
        }
      },
      forget () {
        this.$emit('action', 'forget')
      },
      confirmLogin () {
        this.$refs['login'].validate(valid => {
          if (valid) {
            let param = {
              username: this.loginModel.loginName,
              password: md5(this.loginModel.password),
              api_login: true
            }
            this.$http.post('/login', param).then(res => {
              if (res.result) {
                this.$message.success('登录成功！')
                this.$nextTick(() => {
                  this.$router.push({
                    name: 'welcome'
                  })
                })
                Vue.localStorage.set(Vue.const.COOKIE.KEY_TOKEN, res.data.TOKEN, {
                  maxAge: Vue.const.COOKIE.MAX_AGE
                })
                Vue.localStorage.set(Vue.const.COOKIE.KEY_MENU, res.data.menuList, {
                  maxAge: Vue.const.COOKIE.MAX_AGE
                })
                Vue.localStorage.set(Vue.const.COOKIE.KEY_PERMISSION, res.data.buttonList, {
                  maxAge: Vue.const.COOKIE.MAX_AGE
                })
                document.cookie = `TOKEN=${res.data.TOKEN}`
                this.getUserInfo()
              } else if (res.errorCode === 1004) {
                this.$message.warning(res.msg)
                setTimeout(() => {
                  this.$router.push({name: 'reset'})
                }, 1000)
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        })
      },
      getUserInfo () {
        this.$http.post('/userInfo', {}).then(res => {
          if (res.result) {
            Vue.localStorage.set(Vue.const.COOKIE.USER_INFO, res.data.user, {
              maxAge: Vue.const.COOKIE.MAX_AGE
            })
            console.log('permission: ', Vue.localStorage.get(Vue.const.COOKIE.KEY_PERMISSION))
          }
        }).catch(err => {
          this.$message.error(err)
        })
      }
    },
    mounted () {
    },
    created () {
      this.$nextTick(() => {
        this.$refs['username'].focus()
      })
    }
  }
</script>

<style lang="scss" >
  .login-wrappers {
    width: 300px;
    box-sizing: border-box;
    top: 50%;
    position: absolute;

  .input-title {
    font-size: 24px;
    text-align: left;
    color: inherit;
    margin-bottom: 18px;
  }

  .input-box {
    .box1{
      padding: 0 6px 0 10px;
      border-left: 1px solid #d8dce5;
      cursor: pointer;
      font-size: 13px;
      color: #8f9bb2;
    }
  .box2{
    width: 78px;
    background-repeat: no-repeat !important;
    background-position: center center !important;
    background-size: contain !important;
    cursor: pointer;
  }


  }
  .btn-box{
    .confirm-btn {
      display: block;
      width: 100%;
      padding: 10px 20px;
      background: #2b8dff;
    }
  }

  .forget {
    display: block;
    margin-top: 15px;
    font-size: 13px;
    color: #337ab7;
    cursor: pointer;
  }

  }
  .el-form-item{
    /*margin-bottom: 15px;*/
  }
</style>
